//-----------------------------------------------------------------------
// <copyright file="ResponsibleDAOBase.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Collections;
using MyFramework;
using MyFramework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MIMS.WRS.Entities;

namespace MIMS.WRS.Data
{    
    public abstract class ResponsibleDAOBase
    {
        #region Common methods
        public virtual Responsible CreateResponsibleFromReader(IDataReader reader)
        {
            Responsible item = new Responsible();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("ResponsibleID"))) item.ResponsibleID = (int)reader["ResponsibleID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Name"))) item.Name = (string)reader["Name"];
                if (!reader.IsDBNull(reader.GetOrdinal("ConpointID"))) item.ConpointID = (int)reader["ConpointID"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateResponsibleFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateResponsible methods
            
        public virtual void CreateResponsible(Responsible responsible)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spResponsiblesInsert");
                
                dbCommandWrapper.AddInParameter("@Name", DbType.String, responsible.Name);
                dbCommandWrapper.AddInParameter("@ConpointID", DbType.Int32, responsible.ConpointID);
                dbCommandWrapper.AddOutParameter("@ResponsibleID", DbType.Int32, 0);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
                responsible.ResponsibleID = (int)dbCommandWrapper.GetParameterValue("@ResponsibleID");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateResponsibleException, ex);
            }
        }

        #endregion

        #region UpdateResponsible methods
        
        public virtual void UpdateResponsible(Responsible responsible)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spResponsiblesUpdate");            
                
                dbCommandWrapper.AddInParameter("@ResponsibleID", DbType.Int32, responsible.ResponsibleID);
                dbCommandWrapper.AddInParameter("@Name", DbType.String, responsible.Name);
                dbCommandWrapper.AddInParameter("@ConpointID", DbType.Int32, responsible.ConpointID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateResponsibleException, ex);
            }
        }
        
        #endregion

        #region DeleteResponsible methods
        public virtual void DeleteResponsible(int responsibleID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spResponsiblesDelete");
                
                dbCommandWrapper.AddInParameter("@ResponsibleID", DbType.Int32, responsibleID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteResponsibleException, ex);
            }
        }

        #endregion

        #region GetResponsible methods
        
        public virtual Responsible GetResponsible(int responsibleID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spResponsiblesGet");
                
                dbCommandWrapper.AddInParameter("@ResponsibleID", DbType.Int32, responsibleID);
                
                Responsible responsible =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
                {
                    if (reader.Read())
                    {
                        responsible = CreateResponsibleFromReader(reader);
                        reader.Close();
                    }
                }
                return responsible;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetResponsibleException, ex);
            }
        }
        
        #endregion

        #region GetResponsibles methods
        public virtual ResponsibleCollection GetResponsibles(ResponsibleColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spResponsiblesGetList");
                
                dbCommandWrapper.AddInParameter("@OrderBy", DbType.AnsiString, orderBy.ToString());
                dbCommandWrapper.AddInParameter("@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                dbCommandWrapper.AddInParameter("@Page", DbType.Int32, page);
                dbCommandWrapper.AddInParameter("@PageSize", DbType.Int32, pageSize);
                dbCommandWrapper.AddOutParameter("@TotalRecords", DbType.Int32, 4);
                
                ResponsibleCollection responsibleCollection = new ResponsibleCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
                {
                    while (reader.Read())
                    {
                        Responsible responsible = CreateResponsibleFromReader(reader);
                        responsibleCollection.Add(responsible);
                    }
                    reader.Close();
                }
                totalRecords = (int)dbCommandWrapper.GetParameterValue("@TotalRecords");
                return responsibleCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetResponsiblesException, ex);
            }
        }
        
        public virtual ResponsibleCollection GetResponsibles(ResponsibleColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetResponsibles(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}